import axios from 'axios';

const API_BASE_URL = 'http://127.0.0.1:8000'; // 替换为您的后端API基础URL

const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000, // 设置请求超时时间
});

const getPrecipitationData = (params) => {
  const cancelTokenSource = axios.CancelToken.source();

  const requestConfig = {
    url: '/weather/get_precipitation_data/',
    method: 'get',
    params: params,
    responseType: 'stream', // 设置响应类型为流
    cancelToken: cancelTokenSource.token,
  };

  const responseStream = api(requestConfig).then((response) => {
    return response.data;
  });

  responseStream.cancel = () => {
    // 取消请求
    cancelTokenSource.cancel('Request canceled');
  };

  return responseStream;
};

export { getPrecipitationData };
